Skip to content

test: ensure role gathers the facts it uses by having test clear_facts before include_role#11

Merged
richm merged 1 commit into
linux-system-roles:mainfrom
richm:clear_facts
Apr 10, 2026
Merged

test: ensure role gathers the facts it uses by having test clear_facts before include_role#11
richm merged 1 commit into
linux-system-roles:mainfrom
richm:clear_facts

Conversation

@richm

@richm richm commented Apr 10, 2026

Copy link
Copy Markdown
Contributor

The role gathers the facts it uses. For example, if the user uses
ANSIBLE_GATHERING=explicit, the role uses the setup module with the
facts and subsets it requires.

This change allows us to test this. Before every role invocation, the test
will use meta: clear_facts so that the role starts with no facts.

Create a task file tests/tasks/run_role_with_clear_facts.yml to do the tasks
to clear the facts and run the role. Note that this means we don't need to
use gather_facts for the tests.

Some vars defined using ansible_facts have been changed to be defined with
set_fact instead. This is because of the fact that vars are lazily
evaluated - the var might be referenced when the facts have been cleared, and
will issue an error like ansible_facts["distribution"] is undefined. This is
typically done for blocks that have a when condition that uses ansible_facts
and the block has a role invocation using run_role_with_clear_facts.yml
These have been rewritten to define the when condition using set_fact. This
is because the when condition is evaluated every time a task is invoked in the
block, and if the facts are cleared, this will raise an undefined variable error.

This also updates the CI for the Ansible partner certification checking

Summary by Sourcery

Update CI workflows to run ansible-lint and ansible-test via tox across multiple Ansible/Python versions and adjust tests to clear facts before invoking the trustee_server role.

Enhancements:

  • Introduce a reusable task file to clear facts and run the trustee_server role with optional failure handling.
  • Update generated distro vars metadata to point to the current template source path.

CI:

  • Run ansible-lint via tox with a version matrix for ansible-lint, ansible-core, and Python instead of the dedicated GitHub Action.
  • Run ansible-test sanity checks via tox across a matrix of supported Ansible and Python versions instead of the ansible-test GitHub Action.
  • Upgrade tox-lsr to version 3.18.1 in all affected GitHub workflows.

Tests:

  • Change test playbooks to include the trustee_server role through the new clear-facts task wrapper so tests verify the role gathers its own required facts.
  • Adjust a variable-override test playbook to rely on explicit role execution behavior rather than global fact gathering.

Chores:

  • Regenerate or adjust auxiliary project metadata files such as ansible-lint config and FMF plan definitions.

@richm richm requested a review from spetrosi as a code owner April 10, 2026 14:28
@sourcery-ai

sourcery-ai Bot commented Apr 10, 2026

Copy link
Copy Markdown

Reviewer's Guide

Updates CI workflows to use tox-lsr 3.18.1 and delegate ansible-lint and ansible-test execution to tox-based, matrix-driven jobs across multiple Ansible and Python versions, while adjusting tests to clear facts before running the trustee_server role via a shared task file.

Flow diagram for shared run_role_with_clear_facts Ansible test task

flowchart TD
  subgraph TestPlaybook["Test playbook (e.g. tests_default.yml)"]
    ImportTasks["import_tasks: tests/tasks/run_role_with_clear_facts.yml"]
  end

  subgraph SharedTasks["tests/tasks/run_role_with_clear_facts.yml"]
    ClearFacts["Task: clear_facts"]
    IncludeRole["Task: include_role: trustee_server"]
  end

  ImportTasks --> ClearFacts
  ClearFacts --> IncludeRole

  subgraph TrusteeRole["Role trustee_server"]
    RoleTasks["Role tasks using gathered facts"]
  end

  IncludeRole --> TrusteeRole
  TrusteeRole --> RoleTasks
Loading

File-Level Changes

Change Details Files
Make ansible-lint GitHub workflow matrix-driven and run ansible-lint via tox instead of the GitHub Action.
  • Add a versions matrix for ansible-lint, ansible-core, and Python and disable fail-fast to collect all failures.
  • Upgrade tox-lsr to 3.18.1 and introduce actions/setup-python to pin the Python version from the matrix.
  • Replace custom collection conversion shell logic with a simple tox -e collection invocation.
  • Replace ansible/ansible-lint GitHub Action usage with a tox invocation that passes ansible-lint and ansible-core versions via environment variables and configures basepython for the ansible-lint-collection testenv.
.github/workflows/ansible-lint.yml
Make ansible-test GitHub workflow matrix-driven and run ansible-test via tox instead of the ansible-test GitHub Action.
  • Add a versions matrix for ansible-core channels and Python and disable fail-fast to surface all failures.
  • Upgrade tox-lsr to 3.18.1 and use actions/setup-python to align Python with the matrix entry.
  • Replace inline collection conversion shell script with tox -e collection.
  • Replace ansible-community/ansible-test-gh-action usage with tox-based invocation of ansible-test-* environments, setting basepython per matrix entry.
.github/workflows/ansible-test.yml
Introduce a reusable task file that clears facts before running the trustee_server role and update tests to use it.
  • Add tests/tasks/run_role_with_clear_facts.yml which clears facts with meta: clear_facts, then conditionally runs linux-system-roles.trustee_server with support for __sr_tasks_from, __sr_public, and __sr_failed_when semantics.
  • Use a block/rescue pattern to emulate failed_when: false behavior when running the role while still providing a normal role run path when __sr_failed_when is true or undefined.
  • Update tests_default.yml and tests_secret_registration_server.yml to include the new task file instead of directly including the role, passing __sr_public: true so the role exports variables.
tests/tasks/run_role_with_clear_facts.yml
tests/tests_default.yml
tests/tests_secret_registration_server.yml
Align other CI workflows and test metadata with updated shared tooling and templates.
  • Bump tox-lsr version to 3.18.1 in ansible-managed-var-comment and qemu-kvm-integration-tests workflows to match the rest of CI.
  • Adjust auto-generated rh_distros_vars comment to point to the new template source path.
  • Remove unnecessary gather_facts: true from tests_include_vars_from_parent.yml since facts are now handled explicitly where needed.
  • Include new/updated project files in linting and testing plans (e.g., ansible-lint config and FMF plan changes).
.github/workflows/ansible-managed-var-comment.yml
.github/workflows/qemu-kvm-integration-tests.yml
tests/vars/rh_distros_vars.yml
tests/tests_include_vars_from_parent.yml
.ansible-lint
plans/test_playbooks_parallel.fmf

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • In tests/tasks/run_role_with_clear_facts.yml, consider switching to fully qualified module names (ansible.builtin.meta, ansible.builtin.include_role, ansible.builtin.debug) to align with current ansible-lint expectations and avoid future namespace issues.
  • The new ansible-test matrix includes Python 3.13; if this workflow must run on environments where 3.13 is not yet available in actions/setup-python, you may want to guard or adjust those matrix entries to avoid setup failures.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `tests/tasks/run_role_with_clear_facts.yml`, consider switching to fully qualified module names (`ansible.builtin.meta`, `ansible.builtin.include_role`, `ansible.builtin.debug`) to align with current ansible-lint expectations and avoid future namespace issues.
- The new ansible-test matrix includes Python 3.13; if this workflow must run on environments where 3.13 is not yet available in `actions/setup-python`, you may want to guard or adjust those matrix entries to avoid setup failures.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

…s before include_role

The role gathers the facts it uses.  For example, if the user uses
`ANSIBLE_GATHERING=explicit`, the role uses the `setup` module with the
facts and subsets it requires.

This change allows us to test this.  Before every role invocation, the test
will use `meta: clear_facts` so that the role starts with no facts.

Create a task file tests/tasks/run_role_with_clear_facts.yml to do the tasks
to clear the facts and run the role.  Note that this means we don't need to
use `gather_facts` for the tests.

Some vars defined using `ansible_facts` have been changed to be defined with
`set_fact` instead.  This is because of the fact that `vars` are lazily
evaluated - the var might be referenced when the facts have been cleared, and
will issue an error like `ansible_facts["distribution"] is undefined`.  This is
typically done for blocks that have a `when` condition that uses `ansible_facts`
and the block has a role invocation using run_role_with_clear_facts.yml
These have been rewritten to define the `when` condition using `set_fact`.  This
is because the `when` condition is evaluated every time a task is invoked in the
block, and if the facts are cleared, this will raise an undefined variable error.

Signed-off-by: Rich Megginson <rmeggins@redhat.com>
@richm richm changed the title clear facts test: ensure role gathers the facts it uses by having test clear_facts before include_role Apr 10, 2026
@richm

richm commented Apr 10, 2026

Copy link
Copy Markdown
Contributor Author

[citest]

@richm richm merged commit e1eb7e9 into linux-system-roles:main Apr 10, 2026
37 of 38 checks passed
@richm richm deleted the clear_facts branch April 10, 2026 15:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant